package com.bit.forum.controller;

import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.config.APPConfig;
import com.bit.forum.model.Article;
import com.bit.forum.model.Board;
import com.bit.forum.model.User;
import com.bit.forum.services.IArticleService;
import com.bit.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/article")
@Slf4j
@Api(tags = "帖子接口")
public class ArticleController {

    @Resource
    private IBoardService boardService;

    @Resource
    private IArticleService articleService;
    /**
     * 发布新帖
     * @param boardId 板块id
     * @param title  题目
     * @param content  文章内容
     * @return
     */
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("板块id") @RequestParam("boardId") @NonNull Long boardId,
                            @ApiParam("标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content){
        //参数校验-非空校验已做
        //校验用户是否禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(APPConfig.USER_SESSION);
        if (user.getState() == 1){
            //用户已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //板块校验
        Board board = boardService.selectById(boardId.longValue());
        if (board == null || board.getDeleteState() == 1 || board.getState() == 1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }
        //封装文章对象
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);
        article.setUserId(user.getId());
        articleService.create(article);
        //相应成功
        return AppResult.success();
    }


    /**
     * 查询帖子列表
     * @param boardId
     * @return
     */
    @ApiOperation("获取帖子列表")
    @GetMapping("/getAllByBoardId")
     public AppResult<List<Article>> getAllByBoardId(@ApiParam("板块id")
                                                   @RequestParam(value = "boardId",required = false) Long boardId){

        //定义结果集
        List<Article> articles;
        if (boardId == null){
            articles = articleService.selectAll();

        }else {
            articles = articleService.selectAllByBoardId(boardId);
        }
        //结果是否为空
        if (articles == null){
            //非空判断,创建空集合
            articles = new ArrayList<>();
        }

        return AppResult.success(articles);
     }


     @ApiOperation("根据帖子id获取详情")
     @GetMapping("/getDetails")
     public AppResult<Article> getDetails(HttpServletRequest request,  //获取当前登录用户
                                          @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
         //从session获取当前登录用户
         HttpSession session = request.getSession(false);
         User user = (User) session.getAttribute(APPConfig.USER_SESSION);
        //调用service,获取详情
         Article article = articleService.selectDetailById(id);
         //判断结果是否为空
         if (article == null){
             return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);

         }
         //判断当前用户是否为作者
         if (user.getId() == article.getUserId()){
             article.setOwn(true);
         }
         return AppResult.success(article);
     }



     @ApiOperation("修改帖子")
     @PostMapping("/modify")
     public AppResult modify(HttpServletRequest request,
                             @ApiParam("帖子id") @RequestParam("id") @NonNull Long id,
                             @ApiParam("帖子标题") @RequestParam("title") @NonNull String title,
                             @ApiParam("帖子正文") @RequestParam("content") @NonNull String content){
        //判断用户状态-是否禁言
         HttpSession session = request.getSession(false);
         User user = (User) session.getAttribute(APPConfig.USER_SESSION);
         if (user.getState() == 1){
             //返回错误描述
             return AppResult.failed(ResultCode.FAILED_USER_BANNED);
         }
         //查询帖子详情
         Article article = articleService.selectById(id);
         //判断用户是不是作者
         if (article == null){
             return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
         }
         if (user.getId() != article.getUserId()){
             return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
         }
         //帖子的状态-是否已经截至编辑
         if (article.getState()==1){
             return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
         }
         //调用service
         articleService.modify(id,title,content);
         log.info("更新成功.Article id = "+ id +",user id = "+ user.getId() +".");
         return AppResult.success();
     }


     @ApiOperation("点赞")
     @PostMapping("/thumbsUp")
     public AppResult thumbsUp(HttpServletRequest request,
                               @ApiParam("帖子id") @RequestParam("id") @NonNull  Long id){
        //校验用户状态
         HttpSession session =request.getSession(false);
         User user = (User) session.getAttribute(APPConfig.USER_SESSION);
         //判断是否被禁言
         if (user.getState() == 1){
             return AppResult.failed(ResultCode.FAILED_USER_BANNED);
         }
         //直接调用service
         articleService.thumbsUpById(id);
         return AppResult.success();
     }


     @ApiOperation("删除")
     @PostMapping("/delete")
     public AppResult deleteById(HttpServletRequest request,
                                 @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        //验证用户状态
         HttpSession session = request.getSession(false);
         User user = (User) session.getAttribute(APPConfig.USER_SESSION);
         if (user.getState() == 1){
             return AppResult.failed(ResultCode.FAILED_USER_BANNED);
         }
         //校验帖子状态
         Article article = articleService.selectById(id);
         if (article == null || article.getState() == 1){
             //已删除
             return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
         }
         //用户是不是作者
         if (user.getId() != article.getUserId()){
             return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
         }
         //service
         articleService.deleteById(id);
         //操作成功
         return AppResult.success();
     }


     @ApiOperation("获取用户帖子列表")
     @GetMapping("/getAllByUserId")
     public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
                                     @ApiParam("用户id") @RequestParam(value = "userId",required = false) Long userId){
        //userId为空,从session获取
         if (userId == null){
             HttpSession session = request.getSession(false);
             User user = (User) session.getAttribute(APPConfig.USER_SESSION);
             userId = user.getId();

         }
         List<Article> articles = articleService.selectByUserId(userId);
         return AppResult.success(articles);
     }

}
